//
//  Util.m
//  TestCach
//
//  Created by 钮勇威 on 16/2/19.
//  Copyright © 2016年 阿毛. All rights reserved.
//

#import "Util.h"

@implementation Util


/**
 *  格式化请求URL
 *
 *  @param dic  所有参数
 *
 *  @return 添加v的dictionary
 */
+(NSDictionary *)getDictionaryUrl:(NSDictionary *)dic{
    return [NSDictionary dictionaryWithObjectsAndKeys:[self DataTojsonString:dic],@"v", nil];
}


+(NSString*)DataTojsonString:(id)object
{
    NSString *jsonString = nil;
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object
                                                       options:NSJSONWritingPrettyPrinted // Pass 0 if you don't care about the readability of the generated string
                                                         error:&error];
    if (! jsonData) {
        NSLog(@"Got an error: %@", error);
    } else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return jsonString;
}



+ (NSString*)dictionaryToJson:(NSDictionary *)dic;

{
    if (!dic) {
        return @"";
    }
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding] stringByReplacingOccurrencesOfString:@" " withString:@""];
    
}


/*!
 * @brief 把格式化的JSON格式的字符串转换成字典
 * @param jsonString JSON格式的字符串
 * @return 返回字典
 */
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

//============================================nav主题============================================
+ (NSDictionary *)getNavigationBarTitleTextAttributes {
    NSShadow *shadow = [[NSShadow alloc] init];
    shadow.shadowColor = [UIColor colorWithWhite:1.0 alpha:1];
    NSDictionary *dic = [NSDictionary
                         dictionaryWithObjects:[NSArray arrayWithObjects: RGBJ(44), [UIFont systemFontOfSize:16.0f], shadow, nil]
                         forKeys:[NSArray arrayWithObjects:NSForegroundColorAttributeName, NSFontAttributeName, NSShadowAttributeName, nil]];
    
    return dic;
}
//============================================nav返回============================================
+ (UIBarButtonItem *)addBarButtonItemWithImage:(NSString *)imageName
                                highlightImage:(NSString *)highlightImageName
                                        target:(id)target
                                        action:(SEL)action {
    
    
    UIButton *button               = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame                   = CGRectMake(0, 0, 54,44);
    button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 36);
    UIImage *image = [UIImage imageNamed:imageName];
    [button setImage:image forState:0];
    [button.imageView setContentMode:UIViewContentModeScaleAspectFit];
    if (highlightImageName != nil)
        [button setBackgroundImage:[UIImage imageNamed:highlightImageName] forState:UIControlStateHighlighted];
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:button];
}




+(BOOL)checkTextField:(UIView *)view{
    for(UIView *v in view.subviews){
        if ([v.class  isSubclassOfClass:[UITextField class]]) {
            UITextField *textFild = (UITextField *)v; 
            if (textFild.text.length <= 0) {
                NSLog(@"%@",textFild);
                [textFild becomeFirstResponder];
//                E(textFild.placeholder);
                return NO;
            }
            if (v.subviews) {
                [self checkTextField:v];
            }
        }
    }
    return YES;
}

//判断是否为空
+(BOOL)isNull:(NSString *)inputStr{
    if (inputStr.length == 0 || [inputStr isEqualToString:@"<null>"]  || [inputStr isEqualToString:@"null"]  || [inputStr isEqualToString:@"(null)"] || [inputStr isEqualToString:@"”<null>“"]) {
        return 1;
    }else{
        return 0;
    }
}

//存储全局变量
+(BOOL)addSessionId:(NSString *)sessionId{
    [[NSUserDefaults standardUserDefaults] setObject:sessionId forKey:@"sessionId"];
    return YES;
}

+(NSString *)getSessionId{
    NSString *sessionId = [[NSUserDefaults standardUserDefaults] objectForKey:@"sessionId"];
    return sessionId;
}

+(BOOL)addDepartmentArr:(NSArray *)departmentArr{
    [[NSUserDefaults standardUserDefaults] setObject:departmentArr forKey:@"departmentArr"];
    return YES;
}

+(NSArray *)getDepartmentArr{
    NSArray *departmentArr = [[NSUserDefaults standardUserDefaults] objectForKey:@"departmentArr"];
    return departmentArr;
}

@end
